package com.hfs.fence

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextClock
import android.widget.TextView
import com.amap.api.fence.GeoFence
import com.amap.api.fence.GeoFenceListener
import com.amap.api.location.AMapLocation
import com.amap.api.maps.AMap
import com.amap.api.maps.CameraUpdateFactory
import com.amap.api.maps.LocationSource
import com.amap.api.maps.LocationSource.OnLocationChangedListener
import com.amap.api.maps.MapView
import com.amap.api.maps.model.LatLng
import com.hfs.fence.LocationUtil.ILocationCallBack

class MainActivity : BaseActivity(), LocationSource {
    /**
     * 地图
     */
    private lateinit var mMapView: MapView

    /**
     *  重新定位按钮
     */
    private lateinit var mLocationbtn: ImageView

    /**
     * 签到按钮  用于更换背景颜色
     */
    private lateinit var siginBg: RelativeLayout

    /**
     * 签到提示用于提示打卡状况
     */
    private lateinit var sigin: TextView

    /**
     * 显示当前时间
     */
    private lateinit var timeView: TextClock

    /**
     * 定位监听器
     */
    private var mListener: OnLocationChangedListener? = null
    private var aMap: AMap? = null
    private var locationUtil: LocationUtil? = null

    /**
     * 定位获取的经纬度  添加打卡地址直接用的是这个   正常是后台返回的经纬度
     */
    private var mLat = 0.0
    private var mLgt = 0.0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
        mMapView.onCreate(savedInstanceState)
    }

    override fun onResume() {
        super.onResume()
        mMapView.onResume()
        initData()
    }

    private fun initData() {
        //模拟获取网络时间   用于和本地时间比较避免用户改变手机时间
        val netTime = System.currentTimeMillis()
        //手机当前时间
        val currentTime = System.currentTimeMillis()
        if (locationUtil == null) locationUtil = LocationUtil()
        //判断本地时间和网络时间是否差别过大
        if (compareTime(currentTime, netTime)) {
            //时间正常
            //根据自己后台业务来判断是上下班打卡，迟到早退打卡
            siginBg.visibility = View.VISIBLE
            //如果是迟到或者早退可以换其他颜色背景提示
            siginBg.setBackgroundResource(R.drawable.siginin_bg)
            sigin.text = "上班打卡"
            //时间没问题的话就初始化地图
            initMapView()
        } else {
            //时间不对你可以自己显示另一个页面提示用户
            showToast("手机时间和网络时间相差太大，无法打卡！");
        }
    }

    private fun initMapView() {
        if (aMap == null) {
            aMap = mMapView.map
        }
        //        定位回调
        setLocationCallBack()

        //设置定位监听
        aMap!!.setLocationSource(this)
        //设置缩放级别
        aMap!!.moveCamera(CameraUpdateFactory.zoomTo(16f))
        //显示定位层并可触发，默认false
        aMap!!.isMyLocationEnabled = true
    }

    private fun setLocationCallBack() {
        locationUtil = LocationUtil()
        //定位回调
        locationUtil!!.setLocationCallBack(object : ILocationCallBack {
            override fun callBack(
                str: String?,
                lat: Double,
                lgt: Double,
                aMapLocation: AMapLocation?
            ) {
                mLocationbtn.visibility = View.VISIBLE
                mMapView.visibility = View.VISIBLE
                //隐藏定位显示的圆圈
                locationUtil!!.set(aMap!!)
                //根据获取的经纬度，将地图移动到定位位置
                aMap!!.moveCamera(CameraUpdateFactory.changeLatLng(LatLng(lat, lgt)))
                //添加定位图标
                aMap!!.addMarker(locationUtil!!.getMarkerOption(str, lat, lgt))
                mLat = lat
                mLgt = lgt
                //添加围栏也就是打卡范围  参数1上下文，添加结果回调 ，范围的直径也就是圆圈的直径 ，经纬度
                addCircleGeoFence(this@MainActivity, listener, 1000, lat, lgt)
            }
        })
    }

    //判断围栏是否创建成功
    //添加围栏成功后注册广播监听进出围栏
    private val listener: GeoFenceListener
        get() = GeoFenceListener { list, errorCode, s ->
            if (errorCode == GeoFence.ADDGEOFENCE_SUCCESS) { //判断围栏是否创建成功
                Log.d(TAG, "添加围栏成功!!")
//                locationUtil!!.circle(aMap!!, mLat, mLgt, 300)
                //添加围栏成功后注册广播监听进出围栏
                registerReceiver()
            } else {
                Log.d(TAG, "添加围栏失败!!")
            }
        }

    private fun initView() {
        mMapView = findViewById(R.id.map)
        mLocationbtn = findViewById(R.id.locationbtn)
        siginBg = findViewById(R.id.sigin_bg)
        sigin = findViewById(R.id.sigin)
        timeView = findViewById(R.id.timeView)
        mLocationbtn.setOnClickListener(View.OnClickListener {
            locationUtil!!.startLocate(
                applicationContext
            )
        })
        findViewById<View>(R.id.btn_no_map).setOnClickListener {
            startActivity(Intent(this, NoMapActivity::class.java))
        }
    }

    //    进入围栏
    override fun enter() {}

    //    离开围栏
    override fun out() {}

    override fun onPause() {
        super.onPause()
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        mMapView.onPause()
    }

    override fun activate(onLocationChangedListener: OnLocationChangedListener) {
        mListener = onLocationChangedListener
        locationUtil!!.startLocate(applicationContext)
    }

    override fun deactivate() {}
    override fun onDestroy() {
        super.onDestroy()
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        mMapView.onDestroy()
        locationUtil!!.onDestroy() //销毁定位客户端，同时销毁本地定位服务。
        if (mBReceiver != null) unregisterReceiver(mBReceiver)
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        mMapView.onSaveInstanceState(outState)
    }
}